package com.android.baseconfig.common.views;

import android.app.Activity;
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.FrameLayout;


public class AutoHideIMEFrameLayout extends FrameLayout {

    public AutoHideIMEFrameLayout(Context context) {
        super(context);
    }

    public AutoHideIMEFrameLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public AutoHideIMEFrameLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }


    boolean pendingCollapseKeyword = false;
    View focusedView;

    // 点击非输入框位置优先隐藏软键盘
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        Activity activity = (Activity) getContext();
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            pendingCollapseKeyword = isShouldHideInput(ev);
            if (pendingCollapseKeyword) focusedView = activity.getCurrentFocus();
        } else if (ev.getAction() == MotionEvent.ACTION_UP) {
            if (pendingCollapseKeyword) {
                hideInputMethod(activity);
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    private boolean isShouldHideInput(MotionEvent event) {
        Activity activity = (Activity) getContext();
        View v = activity.getCurrentFocus();
        if (v instanceof EditText) {
            int[] location = {0, 0};
            v.getLocationInWindow(location);
            return event.getX() < location[0]
                    || event.getX() > location[0] + v.getWidth()
                    || event.getY() < location[1]
                    || event.getY() > location[1] + v.getHeight();
        }
        return false;
    }

    // 抬起手指时如果焦点还在原来的EditText则收起键盘
    private void hideInputMethod(Context context) {

        View v = ((Activity)context).getCurrentFocus();
        if (v == focusedView) {
            focusedView.clearFocus();
            InputMethodManager imm = (InputMethodManager)context
                    .getSystemService(Context.INPUT_METHOD_SERVICE);
            if (imm != null) {
                imm.hideSoftInputFromWindow(focusedView.getWindowToken(), 0);
            }
        }
    }


   /* @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            Context context = getContext();
            if (!(context instanceof Activity)) {
                return super.dispatchTouchEvent(ev);
            }

            Activity activity = (Activity) context;
            View focusView = activity.getCurrentFocus();

            if (focusView != null && shouldHideInputMethod(focusView, ev)) {
                hideInputMethod(focusView);
            }
        }

        return super.dispatchTouchEvent(ev);
    }

    private boolean shouldHideInputMethod(View focusView, MotionEvent event) {
        Rect rect = new Rect();
        focusView.getHitRect(rect);
        return !rect.contains((int) event.getX(), (int) event.getY());
    }

    private void hideInputMethod(View currentFocus) {
        if (currentFocus == null) {
            return;
        }
        InputMethodManager imm =
                (InputMethodManager) currentFocus.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(currentFocus.getWindowToken(),
                InputMethodManager.HIDE_NOT_ALWAYS);
    }*/
}